<template>
  <div class="login-container">

    <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left" v-if="step === 1">
      <div class="title-container">
        <h3 class="title">Manuals</h3>
      </div>

      <el-form-item prop="modelName" class="form1">
        <span class="svg-container">
          <el-icon>
            <User />
          </el-icon>
        </span>
        <el-input class="form2" ref="modelName" v-model="loginForm.modelName" placeholder="请输入型号" name="modelName" link tabindex="1" auto-complete="on" />
      </el-form-item>

      <el-form-item prop="brandId" class="form1">
        <span class="svg-container">
          <el-icon>
            <Position />
          </el-icon>
        </span>
<!--        <el-input ref="brandId" v-model="loginForm.brandId" placeholder="请输入用户名" name="brandId" link tabindex="1" auto-complete="on" />-->
        <el-select ref="brandId" v-model="loginForm.brandId" clearable placeholder="品牌" class="form2" >
<!--          <el-option value="1" label="yamaha"/>-->
<!--          <el-option value="2" label="suzuki"/>-->
<!--          <el-option value="3" label="kawasaki"/>-->
<!--          <el-option value="4" label="honda"/>-->
<!--          <el-option value="5" label="bmw"/>-->
<!--          <el-option value="6" label="triumph"/>-->
          <el-option v-for="item in brandList" :label="item.brandName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>


      <el-button size="default" :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" @click.prevent="handleLogin">搜  索</el-button>
    </el-form>
    <div v-if="step === 2">


      <el-table :data="modelList"  class="table" height="90vh">
        <el-table-column prop="name" label="型号名称" />
        <el-table-column prop="name" label="操作" width="80" >
        <template #default="scope">
          <el-button  type="primary" size="small" @click="show({row : scope.row})">提交</el-button>
        </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      v-model="dialogVisible"
      title="请填写接收的邮箱地址"
      width="80vw"
      :before-close="handleClose"
    >
      <el-form
        label-width="1rem"
        :model="formLabelAlign">
        <el-form-item >
          <el-input class="input1" placeholder="请输入邮箱" v-model="formLabelAlign.toUser"/>
        </el-form-item>

      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="toEmial()">
          确认
        </el-button>
      </span>
      </template>
    </el-dialog>

    <el-icon class="icon" @click="goBack()" v-if="step === 2"><ArrowLeftBold /></el-icon>
  </div>
</template>

<script setup lang="ts">
import {onMounted, reactive, ref, toRefs, watch, nextTick, getCurrentInstance, h} from 'vue'

// 组件依赖
import {ElForm, ElInput, ElMessage, ElNotification} from 'element-plus'
import router from '@/router'

// 状态管理依赖
import useStore from '@/store'

import { useRoute } from 'vue-router'

// 组件实例
const { proxy }: any = getCurrentInstance()
const { user } = useStore()
const route = useRoute()

const loginFormRef = ref(ElForm)
const passwordRef = ref(ElInput)

const state = reactive({
  redirect: '',
  loginForm: {
    modelName: '',
    brandId: '',
  },
  loginRules: {
    modelName: [{ required: false, trigger: 'blur' }],
    brandId: [{ required: false, trigger: 'blur'}],
  },
  loading: false,
  passwordType: 'password',
  captchaBase64: '',
  // 大写提示禁用
  capslockTooltipDisabled: true,
  otherQuery: {},
  clientHeight: document.documentElement.clientHeight,
  showCopyright: true,
  step:1,
  modelList:[],
  dialogVisible:false,
  formLabelAlign:{ modelId:'',toUser:'' },
  brandList:[{
    brandName:'',
    id:''
  }]
})

const { loginForm, loginRules, loading, passwordType, captchaBase64, capslockTooltipDisabled, showCopyright,step,modelList,dialogVisible,formLabelAlign,brandList } = toRefs(state)

function handleClose(){
  state.dialogVisible = false
}

function show({row}: { row: any }){
  state.dialogVisible = true
  state.formLabelAlign.modelId = row.id
}

onMounted(()=>{
  loadBrandSelect()
})

async function loadBrandSelect() {
  let res = await proxy.$api.mp.getAllBrand();
  state.brandList = res.data
}

async function toEmial() {
  let res = await proxy.$api.mp.getToEmail(state.formLabelAlign)
  if (res.code != 200){
    ElMessage.error(res.msg)
  }else {
    ElMessage({
      message: '提交成功,请注意查收',
      type: 'success',
    })
  }
  state.dialogVisible = false
}

function goBack(){
  state.step = 1
}

function handleLogin() {
  loginFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      let res = await proxy.$api.mp.search(state.loginForm)
      if (res.code != 200 || res.data.length <= 0){
        ElMessage.error(res.msg)
      }
      state.modelList = res.data.map((x:any) => {
        x.name =  x.modelName+"-"+x.modelYear
        return x
      })
      state.step = 2
    } else {
      return false
    }
  })
}


watch(
  route,
  () => {
    const query = route.query
    if (query) {
      state.redirect = query.redirect as string
      state.otherQuery = getOtherQuery(query)
    }
  },
  {
    immediate: true,
  },
)

function getOtherQuery(query: any) {
  return Object.keys(query).reduce((acc: any, cur: any) => {
    if (cur !== 'redirect') {
      acc[cur] = query[cur]
    }
    return acc
  }, {})
}

onMounted(() => {
  // handleCaptchaGenerate()
  window.onresize = () => {
    if (state.clientHeight > document.documentElement.clientHeight) {
      state.showCopyright = false
    } else {
      state.showCopyright = true
    }
  }
})
</script>

<style lang="scss">
.input1 {
  .el-input__inner{
    color: #606266 !important;
    &::placeholder {
      color: #606266;
    }
  }
}

.icon {
  position: fixed;
  top: 2rem;
  left: 1rem;
  color: #fff;
  font-size: 20px;
}


.table {
  width: 80vw;
  margin: 20px auto;
  //opacity: 0.8;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.1) !important;
  //border-radius: 5px;

  .el-table__cell {
    background: #021b32 !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important ;
    color: #fff;
  }

}

@media only screen and (max-width: 1000px) {
  .form1 {
    .el-form-item__content {
      .svg-container {
      }

      .form2 {
        flex: 1;
        .el-input {
          width: 100%;
          .el-input__wrapper {
            width: 100%;
            .el-input__suffix {
              position: relative;
              right: 10px;
            }
          }
        }
      }
    }
  }

}
</style>

<style lang="scss">
$bg: #021b32;
$light_gray: #fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .el-input {
    display: inline-block;
    height: 36px;
    width: 85%;

    .el-input__wrapper {
      padding: 0;
      background: transparent;
      box-shadow: none;

      .el-input__inner {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        color: $light_gray;
        height: 36px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }

  .el-input__inner {
    &:hover {
      border-color: var(--el-input-hover-border, var(--el-border-color-hover));
      box-shadow: none;
    }

    box-shadow: none;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .copyright {
    width: 100%;
    position: absolute;
    bottom: 0;
    font-size: 12px;
    text-align: center;
    color: #cccccc;
  }
}
</style>

<style lang="scss" scoped>
$bg: #021b32;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 5px 10px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .captcha {
    position: absolute;
    right: 0;
    top: 0;

    img {
      height: 42px;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}
</style>
